package com.e.basebmob;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class MainActivity extends AppCompatActivity {
    private Button btnReg;
    private Button btnLogin;
    private Button btnAdd;
    private Button btnDel;
    private Button btnUpdate;
    private Button btnQuery;
    private Button btnLogout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initBtns();
    }

    private void initBtns() {
        btnAdd = findViewById(R.id.btn_add);
        btnLogin = findViewById(R.id.btn_login);
        btnReg = findViewById(R.id.btn_reg);
        btnDel = findViewById(R.id.btn_del);
        btnUpdate = findViewById(R.id.btn_update);
        btnQuery = findViewById(R.id.btn_query);
        btnLogout = findViewById(R.id.btn_logout);

        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyUser myUser = new MyUser();
                myUser.setUsername("张三");
                myUser.setPassword("123456");
                myUser.setAge(19);
                myUser.setGender("男");
                myUser.signUp(new SaveListener<MyUser>() {
                    @Override
                    public void done(MyUser myUser, BmobException e) {
                        if (e == null) {
                            toast("注册成功" + myUser.getObjectId());
                        } else {
                            toast("注册失败" + e.getMessage());
                        }
                    }
                });
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyUser myUser = new MyUser();
                myUser.setUsername("张三");
                myUser.setPassword("123456");
                myUser.login(new SaveListener<MyUser>() {
                    @Override
                    public void done(MyUser myUser, BmobException e) {
                        if (e == null) {
                            toast("登录成功" + myUser.getUsername());
                        } else {
                            toast("登录失败" + e.getMessage());
                        }
                    }
                });
            }
        });

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Student student = new Student();
                student.setName("陈丽");
                student.setAge(18);
                student.setProfession("软件");
                student.setScore(80);
                student.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if (e == null) {
                            toast("添加成功" + student.getName());
                        }
                    }
                });
            }
        });

        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
                studentBmobQuery.addWhereEqualTo("age", 18);
                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                        if (e == null) {
                            for (Student student : list) {
                                student.delete(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e == null) {
                                            toast("删除成功");
                                        } else {
                                            toast("删除失败" + e.getMessage());
                                        }
                                    }
                                });
                            }
                        }
                    }
                });
            }
        });

        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
                studentBmobQuery.addWhereEqualTo("name", "赵武");
                studentBmobQuery.findObjects(new FindListener<Student>() {
                    @Override
                    public void done(List<Student> list, BmobException e) {
                        if (e == null) {
                            StringBuffer stringBuffer = new StringBuffer();
                            for (Student student : list) {
                                stringBuffer.append(student).append("\n");
                            }
                            toast(stringBuffer.toString());
                        }
                    }
                });
            }
        });

        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
                if (myUser == null) {
                    toast("请先登录");
                    return;
                }

                myUser.setGender("男");
                myUser.setAge(21);
                myUser.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            toast("用户信息修改成功");
                        } else {
                            toast("用户信息修改失败" + e.getMessage());
                        }
                    }
                });
            }
        });

        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobUser.logOut();
                toast("退出登录");
            }
        });
    }


    private void toast(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    }
}
